﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Stopwatch.Net
{
    public partial class MainWindow : Window
    {
        private DispatcherTimer dispatcher = new DispatcherTimer();
        private System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
        private BitmapImage stop = new BitmapImage(new Uri("pack://application:,,,/Stopwatch.Net;component/Images/Stop.png"));
        private BitmapImage play = new BitmapImage(new Uri("pack://application:,,,/Stopwatch.Net;component/Images/Play.png"));

        public MainWindow()
        {
            InitializeComponent();            
            dispatcher.Tick += new EventHandler(Process);
            dispatcher.Interval = TimeSpan.FromMilliseconds(10);

            Left = (SystemParameters.PrimaryScreenWidth - MainWnd.Width) - 10;
            Top = (SystemParameters.PrimaryScreenHeight - MainWnd.Height) - 50;
        }

        private void Process(object sender, EventArgs e)
        {
            LbTime.Content = stopwatch.Elapsed.ToString(@"hh\:mm\:ss\.ff");
        }
      
        private void MainWnd_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (!stopwatch.IsRunning)
            {
                AppImage.Source = stop;
                LbLast.Content = LbTime.Content;
                LbTime.Content = "00:00.00";
                stopwatch.Reset();
                stopwatch.Start();
                dispatcher.Start();
            }
            else
            {
                AppImage.Source = play;
                dispatcher.Stop();
                stopwatch.Stop();
            }
        }
    }
}
